<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
      </el-row>
      <!-- 表格 -->
      <tree-table :data="catelist" :columns="columns" :show-index="true" borderm :selection-type="false" :expand-type="false" index-text="#">
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: lightgreen"></i>
          <i class="el-icon-error" v-if="scope.row.cat_deleted === true" style="color: red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success" size="mini">二级</el-tag>
          <el-tag v-else type="warning" suze="mini">三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button size="mini" type="primary" @click="editCateName(scope)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteCateName(scope)">删除</el-button>
        </template>
      </tree-table>
      <!-- 分类区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisble" width="50%" @close='addCateDialogClosed'>
      <!-- 添加分类表单 -->
      <el-form :model="addCateFrom" :rules="addCateRules" ref="addCateRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateFrom.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类"></el-form-item>
        <!-- 级联选择器 -->
        <!-- option 制定数据源 -->
        <!-- props级联选择器的选择 -->
        <el-cascader v-model="selectedKeys" :options="parentCateList" :props="cascaderProps" clearable
        @change="parentCateChanged"></el-cascader>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisble = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
     <!-- 编辑分类的 -->
    <el-dialog title="修改名称" :visible.sync="editCateDialogVisble" width="50%" @close='editCateDialogClosed'>
      <!-- 添加分类表单 -->
      <el-form :model="editCateFrom" ref="editCateRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editCateFrom.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialogVisble = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 请求商品携带数据
      queryinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 总的数据条数
      total: 0,
      // 存放商品数据
      catelist: [],
      // 绑定对话框是否显示
      addCateDialogVisble: false,
      // 显示数据的
      editCateDialogVisble: false,
      // 表格绑定数据规则
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 表示，将定义为模板列
          type: 'template',
          // 表示当前这一列使用模板名称
          template: 'isok'
        },
        {
          label: '排序',
          // 表示，将定义为模板列
          type: 'template',
          // 表示当前这一列使用模板名称
          template: 'order'
        },
        {
          label: '操作',
          // 表示，将定义为模板列
          type: 'template',
          // 表示当前这一列使用模板名称
          template: 'opt'
        }
      ],
      // 添加分类表单存储数据
      addCateFrom: {
        cat_name: '',
        // 分类id
        cat_level: 0,
        cat_pid: 0
      },
      // editCateFrom 编辑绑定数据
      editCateFrom: {
        cat_name: '',
        cat_id: ''
      },
      // 添加分表验证的数据
      addCateRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      // 对话框获得的数据 父级分类的数据
      parentCateList: [],
      // 级联选择器的配置对象
      cascaderProps: { expandTrigger: 'hover', value: 'cat_id', label: 'cat_name', children: 'children', checkStrictly: true },
      // 级联选择器的 选中存放的数据
      selectedKeys: []
    }
  },
  created () {
    // 加载页面之前请求商品数据
    this.getCateList()
  },
  methods: {
    // 请求商品列表数据
    async getCateList () {
      const { data: res } = await this.$http.get('categories', { params: this.queryinfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获得商品数据失败')
      }
      //   console.log(res.data)  //打印查看data 的数据结构
      this.catelist = res.data.result
      // console.log(this.catelist)
      this.total = res.data.total
    },
    // 监听pagesize改变
    handleSizeChange (newSize) {
      this.queryinfo.pagesize = newSize
      this.getCateList()
    },
    // 监听pagenumber改变
    handleCurrentChange (newPage) {
      this.queryinfo.pagenum = newPage
      this.getCateList()
    },
    // 添加显示对话框
    showAddCateDialog () {
      this.getParentCateList()
      this.addCateDialogVisble = true
      // console.log(this.parentCateList)
    },
    // 打开对话框之前获取父级分类
    async getParentCateList () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      if (res.meta.status !== 200) {
        return this.$message.error('请求数据失败')
      }
      this.parentCateList = res.data
    },
    // 级联选择器父选项改变
    parentCateChanged () {
      // console.log(this.selectedKeys)
      if (this.selectedKeys.length > 0) {
        this.addCateFrom.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.addCateFrom.cat_level = this.selectedKeys.length
        return 0
      } else {
        this.addCateFrom.cat_pid = 0
        // 为当前分类的等级赋值
        this.addCateFrom.cat_level = 0
      }
    },
    addCate () {
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addCateFrom)
        if (res.meta.status !== 201) return this.$message.error('失败')
        this.$message.success('添加成功')
      })
      this.addCateDialogVisble = false
      this.getCateList()
    },
    // 关闭对话框后重置表单
    addCateDialogClosed () {
      this.$refs.addCateRef.resetFields()
      this.selectedKeys = []
    },
    // 打开编辑对话框后
    editCateName (scope) {
      this.editCateDialogVisble = true
      // console.log(scope.row)
      this.editCateFrom.cat_name = scope.row.cat_name
      this.editCateFrom.cat_id = scope.row.cat_id
    },
    // 关闭编辑对话框后
    editCateDialogClosed () {
      this.$refs.editCateRef.resetFields()
      this.editCateName.cat_id = 0
      this.editCateName.cat_name = ''
    },
    // 编辑提交内容
    async editCate () {
      const { data: res } = await this.$http.put('categories/' + this.editCateFrom.cat_id, { cat_name: this.editCateFrom.cat_name })
      // console.log({ cat_name: this.editCateFrom.cat_name })
      if (res.meta.status !== 200) return this.$message.error('错误')
      this.$message.success('更新成功')
      this.getCateList()
      this.editCateDialogVisble = false
    },
    // 删除一个用户
    async deleteCateName (scope) {
      const { data: res } = await this.$http.delete('categories/' + scope.row.cat_id)
      if (res.meta.status !== 200) return this.$message.error('错误')
      this.$message.success('删除成功')
      this.getCateList()
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader{
  width: 100%;
}
</style>
